<?php

use think\migration\Migrator;
use app\model\PageSetting;

class AlterPageSettingChangeExtDataByJumpType extends Migrator
{
    public function up()
    {
        $affectedNum = 0;
        PageSetting::query()
            //->where('status', PageSetting::STATUS_ACTIVE)
            ->whereRaw('ext_data like "%/takeout/ele%" OR ext_data like "%/takeout/meituan%"')
            ->select()->each(function (PageSetting $pageSetting) use (&$affectedNum){
                $new_ext_data = $ext_data = $pageSetting->ext_data;
                if (isset($ext_data['jump_type']) && !empty($jump_type = $ext_data['jump_type'])) {
                    if ($jump_type == 1) {
                        $new_ext_data['jump_type'] = PRODUCT_ELE_SERV;
                    } elseif($jump_type == 2) {
                        $new_ext_data['jump_type'] = PRODUCT_MEITUAN_TAKEOUT_SERV;
                    }
                    $pageSetting->ext_data = $new_ext_data;
                    $pageSetting->save();
                    $affectedNum++;
                }
            });
        echo '影响行数:'.$affectedNum;
    }
}
